package com.demo.connectdbtest.module.ssh.controller;

import org.springframework.boot.loader.jar.JarFile;

import java.io.BufferedReader;
import java.io.File;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarEntry;
import java.util.Collection;

public class JarController {

    //jarPath参数替换为你的Jar包的路径
    public static List<Class> getAllClassesFromJar(String jarPath) throws IOException, ClassNotFoundException {
        List<Class> classes = new ArrayList<>();
        File jarFile = new File(jarPath);
        JarFile jar = new JarFile(jarFile);
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            System.out.println("entryName=" + entry.getName());
            if (entry.getName().endsWith(".class")) {
                String className = entry.getName().replace("/", ".").substring(0, entry.getName().length() - 6);
                Class<?> clazz = Class.forName(className);
                classes.add(clazz);
            }
            if (entry.getName().endsWith(".jar")) {
                System.out.println("jar包名称=" + entry.getName());
            }
        }
        return classes;
    }


    /**
     * success 解压jar包
     * @param args
     * @throws IOException
     * @throws FileNotFoundException
     */
    public static void main(String[] args) throws IOException, FileNotFoundException {
        try {
            String path = "D:\\data\\testClasses\\";
            File dir =new File(path);
             if (!dir.exists()) {
                 dir.mkdirs();
            }
            String jarPath = path + "passenger-management-bootstrap-3.1.2-SNAPSHOT.jar";
           // String jarPath = path + "passenger-management-bootstrap-V3.1.3.113.jar";
            StringBuilder cmd = new StringBuilder();
            cmd.append("cmd /c cd " + path + " && jar -xvf " + jarPath);
            Process exec = Runtime.getRuntime().exec(String.valueOf(cmd));
//            exec.waitFor(20000, TimeUnit.SECONDS);
//            exec.waitFor();
            //输出是为了解决卡住问题
            InputStream in = exec.getInputStream();
            InputStreamReader inReader = new InputStreamReader(in, StandardCharsets.UTF_8);
            BufferedReader inBr = new BufferedReader(inReader);
            String strIn;
            while ((strIn = inBr.readLine()) != null) {
                System.out.println(strIn);
            }

            FileUtils.copyDirectoryToDirectory(new File(path + "\\BOOT-INF\\classes\\com"), new File(path));


            //true 代表递归 false 代表不递归
            Collection<File> files = FileUtils.listFiles(new File(path + "BOOT-INF\\lib\\"), null, false);
            StringBuilder executeCmd = new StringBuilder();
            executeCmd.append("cmd /c cd " + path);
            for (File file : files) {
                System.out.println(file.getName());
                if (file.getName().contains("passenger")) {
                    FileUtils.copyFileToDirectory(file, new File(path));
                    executeCmd.append(" &&  jar -xvf " + path + "\\" + file.getName());
                }
            }
            System.out.println("executeCmd命令：" + executeCmd.toString());
            Process result = Runtime.getRuntime().exec(String.valueOf(executeCmd));
//            System.out.println(result);
//            System.out.println(exec);
            InputStream inputStream = result.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String strInput;
            while ((strInput = bufferedReader.readLine()) != null) {
                //输出是为了获取最新的文件
                System.out.println("准备复制="+strInput);
            }

            //复制字节码文件到其他的地方
            FileUtils.copyDirectoryToDirectory(new File(path + "com"), new File("E:\\data\\uncompressClasses\\passenger\\"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
